//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var that = this
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;//发送给服务器的code
        wx.getUserInfo({
          // withCredentials: false,
          success: function (option) {
            that.globalData.userInfo = option.userInfo;
            var userNick = option.userInfo.nickName;//用户昵称
            var avataUrl = option.userInfo.avatarUrl;//用户头像地址
            var gender = option.userInfo.gender;//用户性别
            if (code) {
              wx.request({
                url: that.globalData.apiUrl+'api/wechat',
                //服务器的地址，现在微信小程序只支持https请求，所以调试的时候请勾选不校监安全域名
                data: {
                  code: code,
                  nickname: userNick,
                  avatar: avataUrl,
                  gender: gender,
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res_callback) {
                  that.globalData.openid = res_callback.data.openid;
                  // console.log('openid:' + that.globalData.openid);
                  // wx.setStorageSync('name', res_callback.data.nickname);//将获取信息写入本地缓存
                  wx.setStorageSync('openid', res_callback.data.openid);
                  // wx.setStorageSync('imgUrl', res_callback.data.avatar);
                  // wx.setStorageSync('sex', res_callback.data.gender);
                }
              })

            } else {
              wx.showToast({
                title: '获取信息失败，请刷新重试',
                icon: 'success',
                duration: 2000
              })
            }
          },
          fail: function(option2){
            console.log('Get Userinfo fail');
            
          }
        })
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log('setting')
              console.log(this.globalData.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          console.log('setting cancel')
        }
      },
      fail: res => {
        console.log('no setting')
        if (!app.globalData.userInfo) {
          wx.redirectTo({
            url: "/pages/loginx/index"
          })
        }
      }
    })
    
    
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      // 调用登录接口
      wx.login({
        success: function (res) {
          var code = res.code
          // success
          // 获取用户信息
          wx.getUserInfo({
            success: function (data) {
              that.globalData.userInfo = data.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
              var rawData = data.rawData;
              var signature = data.signature;
              var encryptedData = data.encryptedData;
              var iv = data.iv;
              wx.request({
                url: that.globalData.apiUrl + "api/wechat",
                data: {
                  "code": code,
                  "rawData": rawData,
                  "signature": signature,
                  "iv": iv,
                  "encryptedData": encryptedData
                },
                method: 'GET',
                success: function (res) {
                  // success
                  console.log(res)
                  console.log(rawData)
                }
              })
            }
          })
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    openid: null,
    apiUrl: "https://70.daokeyun.com/index.php/",
    serverUrl: "https://70.daokeyun.com/",
    vrurl: "https://70vr.daokeyun.com/"
  }
})